<?php
if($_SERVER['REQUEST_METHOD'] === 'POST'){
    //清理PHP缓存
    if(function_exists("opcache_reset")){
        opcache_reset();
    }
    //检查是否已安装
    if (file_exists('./system') && is_dir('./system')) {
        msg(-1,'不可重复安装');
    }
    //在线安装 
    if(!empty($_POST['url'])){
        //删除文件
        @unlink('./index.php');
        @unlink('./install.tar.gz');
        $down = downFile($_POST['url'],'install.tar.gz');
        if($down == false){
            msg(0,'下载安装包失败!请重试或手动安装!');
        }
        try {
            $filePath = "./install.tar.gz";
            $phar = new PharData($filePath);
            $phar->extractTo('./', null, true);
            unlink($filePath);
        }catch(Exception $e) {
            msg(0,'释放更新包,请检查写入权限');
        }
        unlink('./install.php');
        msg(1,'正在跳转到安装页面...');
    }
    msg(0,'无法识别的POST请求');
}

function msg($code,$msg){
    $data = ['code'=>$code,'msg'=>$msg];
    header('Content-Type:application/json; charset=utf-8');
    exit(json_encode($data));
}
function downFile($url, $file = '', $savePath = './'){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_TIMEOUT, 120);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_NOBODY, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_REFERER, 'http://tznb.gitee.io/');
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36');
    try{
        $res = curl_exec($ch);
    }finally{
        $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
    }
    if ($code == '200') {
        $fullName = rtrim($savePath, '/') . '/' . $file;
        return file_put_contents($fullName, $res) > 0;
    }else{
        return false;
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>安装指引 - TwoNav</title>
    <link href="//n1.lm21.top/Layui/v2.9.8/css/layui.css" rel="stylesheet">
    <style>*{ margin: 0; padding: 0; color: #31313f; } .error{ width: 730px; margin: 10% auto; font-size: 14px; background-color:#f3f7f9 ; border: 1px solid #cacad9; height: auto; } .title{ background-color: #f44336; color: #fff; height: 46px; line-height: 46px; padding-left: 20px; } .content{ padding: 35px 22px 50px 22px; } .content .result{ color: #f34335;padding-bottom: 10px; }a{ color: #3da9c2;text-decoration: none;}</style>
</head>
<body>
    <div class="error">
        <p class="title">安装指引</p>
        <div class="content">
            <p class="result">相关资料: 
			    <a href="https://gitee.com/tznb/TwoNav" target="_blank">项目地址</a> | 
			    <a href="https://gitee.com/tznb/TwoNav/wikis/pages" target="_blank">使用文档</a> | 
			    <a href="https://gitee.com/tznb/TwoNav/wikis/pages?sort_id=7968667&doc_id=3767990" target="_blank">手动安装教程</a> | 
			    <a href="https://gitee.com/tznb/TwoNav/wikis/pages?sort_id=7968669&doc_id=3767990" target="_blank">购买授权</a>
			</p>
			<p class="result">技术支持: QQ 271152681</p>
			<p class="result">技术交流群1(会员): 695720839</p>
			<p class="result">技术交流群2(免费): 621815595</p>
			<p class="result">Docker镜像日期: 2024.05.13</p>
			<div class="layui-form-item">
                <button class="layui-btn layui-btn-fluid layui-btn-normal" id="install1" lay-filter="install1" style="display:none;">在线安装 ( 自动下载安装包 )</button>
            </div>
        </div>
    </div>
    <script src="//n1.lm21.top/Layui/v2.9.8/layui.js"></script>
    <script src="//n1.lm21.top/install/install.js"></script>
</body>
</html>